﻿import QtQuick 2.9
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.2
import QtQuick.Controls.Material 2.1
import QtQuick.Window 2.0
import QtQuick.Particles 2.0
import QtGraphicalEffects 1.0  // 导入这个模块

ApplicationWindow {
    id: mainWindow
    visible: true
    width: 810
    height: 600
    title: qsTr("智能访客管理平台")
    flags: Qt.FramelessWindowHint | Qt.Window | Qt.WindowMinMaxButtonsHint

    header: ToolBar {
        id: header
        Material.foreground: "#3c3c3c"
        property int nTab: 0
        height: 68

        MouseArea { //为窗口添加鼠标事件
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton //只处理鼠标左键

            property point clickPos: "0,0"
            onPressed: {
                clickPos  = Qt.point(mouse.x,mouse.y)
            }
            onPositionChanged: {
                if (mainWindow.visibility == Window.Maximized)
                    return;

                var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
                mainWindow.x=(mainWindow.x+delta.x)
                mainWindow.y=(mainWindow.y+delta.y)
            }
            onDoubleClicked: {
                if (mainWindow.visibility == Window.Maximized)
                    mainWindow.visibility = Window.Windowed
                else
                    mainWindow.visibility = Window.Maximized
            }
        }


        background: Rectangle
                    {
                        gradient: Gradient {
                            GradientStop { position: 0.0; color: "#484848" }
                            GradientStop { position: 1.0; color: "#383838" }
                        }
                    }

        RowLayout {
            spacing: 0
            anchors.fill: parent
            anchors.leftMargin: 10
            IconFont {
                id: logo
                width: 26
                height: 26
                source: "\uf073"
                color: "#dcdcdc"
            }

            Label {
                id: titleLabel
                leftPadding: 10
                text: qsTr("智能访客管理平台")
                color: "#dcdcdc"
                font.pixelSize: 26
                Layout.minimumWidth: 250
                Layout.minimumHeight: header.height
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignLeft
                verticalAlignment: Qt.AlignVCenter
            }

            NavBtn {
                nTab: 0
                strText: qsTr("主界面")
                icon: "./image/main_main.png"
                nCurTab: header.nTab
                Layout.minimumHeight: header.height
                Layout.minimumWidth: 86
                onClicked: {
                    if (0 == header.nTab)
                        return;

                    header.nTab = 0
                    stackView.pop()
                }
            }

            NavBtn {
                nTab: 1
                strText: qsTr("系统设置")
                icon: "./image/main_config.png"
                Layout.minimumHeight: header.height
                Layout.minimumWidth: 86
                nCurTab: header.nTab
                onClicked: {
                    if (1 == header.nTab)
                        return;

                    header.nTab = 1
                    stackView.pop()
                    stackView.push(pane2)
                }
            }

            NavBtn {
                nTab: 2
                strText: qsTr("调试帮助")
                icon: "qrc:/image/main_person.png"
                Layout.minimumHeight: header.height
                Layout.minimumWidth: 86
                nCurTab: header.nTab
                onClicked: {
                    if (2 == header.nTab)
                        return;

                    header.nTab = 2
                    stackView.pop()
                    stackView.push(pane3)
                }
            }

            Label {
                Layout.fillWidth: true
            }

            Pane {
                padding: 0
                Layout.minimumHeight: header.height
                background: Rectangle
                            {
                                gradient: Gradient {
                                    GradientStop { position: 0.0; color: "#484848" }
                                    GradientStop { position: 1.0; color: "#383838" }
                                }
                            }

                ColumnLayout {
                    spacing: 0
                    anchors.fill: parent   //填充到父项

                    RowLayout {
                        spacing: 0
                        anchors.fill: parent

                        IconBtn {
                            Layout.preferredWidth: 26
                            Layout.preferredHeight: 26
                            icon: "\uf068"
                            onClicked: mainWindow.visibility = Window.Minimized
                        }

                        IconBtn {
                            Layout.preferredWidth: 26
                            Layout.preferredHeight: 26
                            icon: (mainWindow.visibility == Window.Maximized) ? "\uf2d2" : "\uf067"
                            onClicked: {
                                if (mainWindow.visibility == Window.Maximized)
                                    mainWindow.visibility = Window.Windowed
                                else
                                    mainWindow.visibility = Window.Maximized
                            }
                        }

                        IconBtn {
                            iconHoveredBgColor: "#9a2323"
                            iconPressedBgColor: "#9a2323"
                            Layout.preferredWidth: 26
                            Layout.preferredHeight: 26
                            icon: "\uf00d"
                            onClicked: {
                                Qt.quit()
                            }
                        }
                    }


                    Label {
                        Layout.fillHeight: true
                    }
                }
            }

        }
    }

    StackView {
        id: stackView
        anchors.fill: parent

        background: Rectangle
                    {
                        color:"#3f3f3f"
                    }
         initialItem: Home {
            id: pane1
         }
    }
    Component  {
        id: pane2
        Config {
        }
    }
    Component  {
        id: pane3
        Help {
        }
    }

    // 粒子特效，下雪
    ParticleSystem {
        anchors.fill: parent
        Emitter {
            width: parent.width
            emitRate: 20
            lifeSpan: parent.height * 1000 / 60 + 4000
            velocity: PointDirection { x: 1; xVariation: 5; y: 60 }
            size: 1                 // 大小
            sizeVariation: 16        // 大小变化区间
        }
        Wander {
            anchors.fill: parent
            anchors.bottomMargin: 50
            xVariance: 60
            pace: 60
        }

        //! [0]
        Affector {
            property real coefficient: 0.1
            property real velocity: 1.5
            width: parent.width
            height: parent.height - 50
            onAffectParticles: {
                //Wobbly movement
                for (var i=0; i<particles.length; i++) {
                    var particle = particles[i];
                    if (particle.r == 0.0) {
                        particle.r = Math.random() + 0.01;
                    }
                    particle.rotation += velocity * particle.r * dt;
                    particle.r -= particle.rotation * coefficient;
                    if (particle.r == 0.0)
                        particle.r -= particle.rotation * 0.000001;
                    particle.update = 1;
                }
            }
        }
        //! [0]

        //! [1]
        Affector {//Custom Friction, adds some 'randomness'
            x: -10
            width: parent.width + 120
            height: 10
            anchors.bottom: parent.bottom
            onAffectParticles: {
                for (var i=0; i<particles.length; i++) {
                    var particle = particles[i];
                    var pseudoRand = (Math.floor(particle.t*1327) % 10) + 1;
                    var yslow = dt * pseudoRand * 50 + 1;
                    var xslow = dt * pseudoRand * 0.05 + 1;
                    if (particle.vy < 1)
                        particle.vy = 0;
                    else
                        particle.vy = (particle.vy / yslow);
                    if (particle.vx < 1)
                        particle.vx = 0;
                    else
                        particle.vx = (particle.vx / xslow);
                    particle.update = true;
                }
            }
        }
        //! [1]

        ImageParticle {
            anchors.fill: parent
            id: particles
            color: "#00444444"
            sprites: [Sprite {
                    source: "image/snowflake.png"
                    frameCount: 1
                    frameDuration: 1
                    to: {"a":1, "b":1, "c":1, "d":1}
                }]

            z:4
        }
    }
}
